home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tcl / tclUnixTime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-01  |  4.9 KB  |  218 lines

  1. /* 
  2.  * tclUnixTime.c --
  3.  *
  4.  *    Contains Unix specific versions of Tcl functions that
  5.  *    obtain time values from the operating system.
  6.  *
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tclUnixTime.c 1.10 96/02/15 11:58:41
  13.  */
  14.  
  15. #include "tclInt.h"
  16. #include "tclPort.h"
  17.  
  18. /*
  19.  *-----------------------------------------------------------------------------
  20.  *
  21.  * TclGetSeconds --
  22.  *
  23.  *    This procedure returns the number of seconds from the epoch.  On
  24.  *    most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
  25.  *
  26.  * Results:
  27.  *    Number of seconds from the epoch.
  28.  *
  29.  * Side effects:
  30.  *    None.
  31.  *
  32.  *-----------------------------------------------------------------------------
  33.  */
  34.  
  35. unsigned long
  36. TclGetSeconds()
  37. {
  38.     return time((time_t *) NULL);
  39. }
  40.  
  41. /*
  42.  *-----------------------------------------------------------------------------
  43.  *
  44.  * TclGetClicks --
  45.  *
  46.  *    This procedure returns a value that represents the highest resolution
  47.  *    clock available on the system.  There are no garantees on what the
  48.  *    resolution will be.  In Tcl we will call this value a "click".  The
  49.  *    start time is also system dependant.
  50.  *
  51.  * Results:
  52.  *    Number of clicks from some start time.
  53.  *
  54.  * Side effects:
  55.  *    None.
  56.  *
  57.  *-----------------------------------------------------------------------------
  58.  */
  59.  
  60. unsigned long
  61. TclGetClicks()
  62. {
  63.     unsigned long now;
  64. #ifdef NO_GETTOD
  65.     struct tms dummy;
  66. #else
  67.     struct timeval date;
  68.     struct timezone tz;
  69. #endif
  70.  
  71. #ifdef NO_GETTOD
  72.     now = (unsigned long) times(&dummy);
  73. #else
  74.     gettimeofday(&date, &tz);
  75.     now = date.tv_sec*1000000 + date.tv_usec;
  76. #endif
  77.  
  78.     return now;
  79. }
  80.  
  81. /*
  82.  *----------------------------------------------------------------------
  83.  *
  84.  * TclGetTimeZone --
  85.  *
  86.  *    Determines the current timezone.  The method varies wildly
  87.  *    between different platform implementations, so its hidden in
  88.  *    this function.
  89.  *
  90.  * Results:
  91.  *    Hours east of GMT.
  92.  *
  93.  * Side effects:
  94.  *    None.
  95.  *
  96.  *----------------------------------------------------------------------
  97.  */
  98.  
  99. int
  100. TclGetTimeZone (currentTime)
  101.     unsigned long  currentTime;
  102. {
  103.     /*
  104.      * Determine how a timezone is obtained from "struct tm".  If there is no
  105.      * time zone in this struct (very lame) then use the timezone variable.
  106.      * This is done in a way to make the timezone variable the method of last
  107.      * resort, as some systems have it in addition to a field in "struct tm".
  108.      * The gettimeofday system call can also be used to determine the time
  109.      * zone.
  110.      */
  111.     
  112. #if defined(HAVE_TM_TZADJ)
  113. #   define TCL_GOT_TIMEZONE
  114.     time_t      curTime = (time_t) currentTime;
  115.     struct tm  *timeDataPtr = localtime(&curTime);
  116.     int         timeZone;
  117.  
  118.     timeZone = timeDataPtr->tm_tzadj  / 60;
  119.     if (timeDataPtr->tm_isdst) {
  120.         timeZone += 60;
  121.     }
  122.     
  123.     return timeZone;
  124. #endif
  125.  
  126. #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
  127. #   define TCL_GOT_TIMEZONE
  128.     time_t     curTime = (time_t) currentTime;
  129.     struct tm *timeDataPtr = localtime(¤tTime);
  130.     int        timeZone;
  131.  
  132.     timeZone = -(timeDataPtr->tm_gmtoff / 60);
  133.     if (timeDataPtr->tm_isdst) {
  134.         timeZone += 60;
  135.     }
  136.     
  137.     return timeZone;
  138. #endif
  139.  
  140.     /*
  141.      * Must prefer timezone variable over gettimeofday, as gettimeofday does
  142.      * not return timezone information on many systems that have moved this
  143.      * information outside of the kernel.
  144.      */
  145.     
  146. #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
  147. #   define TCL_GOT_TIMEZONE
  148.     static int setTZ = 0;
  149.     int        timeZone;
  150.  
  151.     if (!setTZ) {
  152.         tzset();
  153.         setTZ = 1;
  154.     }
  155.  
  156.     /*
  157.      * Note: this is not a typo in "timezone" below!  See tzset
  158.      * documentation for details.
  159.      */
  160.  
  161.     timeZone = timezone / 60;
  162.  
  163.     return timeZone;
  164. #endif
  165.  
  166. #if defined(HAVE_GETTIMEOFDAY) && !defined (TCL_GOT_TIMEZONE)
  167. #   define TCL_GOT_TIMEZONE
  168.     struct timeval  tv;
  169.     struct timezone tz;
  170.     int timeZone;
  171.  
  172.     gettimeofday(&tv, &tz);
  173.     timeZone = tz.tz_minuteswest;
  174.     if (tz.tz_dsttime) {
  175.         timeZone += 60;
  176.     }
  177.     
  178.     return timeZone;
  179. #endif
  180.  
  181. #ifndef TCL_GOT_TIMEZONE
  182.     /*
  183.      * Cause compile error, we don't know how to get timezone.
  184.      */
  185.     error: autoconf did not figure out how to determine the timezone. 
  186. #endif
  187.  
  188. }
  189.  
  190. /*
  191.  *----------------------------------------------------------------------
  192.  *
  193.  * TclGetTime --
  194.  *
  195.  *    Gets the current system time in seconds and microseconds
  196.  *    since the beginning of the epoch: 00:00 UCT, January 1, 1970.
  197.  *
  198.  * Results:
  199.  *    Returns the current time in timePtr.
  200.  *
  201.  * Side effects:
  202.  *    None.
  203.  *
  204.  *----------------------------------------------------------------------
  205.  */
  206.  
  207. void
  208. TclGetTime(timePtr)
  209.     Tcl_Time *timePtr;        /* Location to store time information. */
  210. {
  211.     struct timeval tv;
  212.     struct timezone tz;
  213.     
  214.     (void) gettimeofday(&tv, &tz);
  215.     timePtr->sec = tv.tv_sec;
  216.     timePtr->usec = tv.tv_usec;
  217. }
  218.